package demo.zzz.com.captureactivity;

import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

//import com.acker.simplezxing.activity.CaptureActivity;

import com.acker.simplezxing.activity.CaptureActivity;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

public class simpleZxingActivity extends AppCompatActivity {
    private static final String TAG="simpleZxingActivity";
    private String mSealData = null;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_simple_zxing );
        if (ContextCompat.checkSelfPermission( simpleZxingActivity.this, Manifest.permission.CAMERA ) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions( simpleZxingActivity.this, new String[]{Manifest.permission.CAMERA}, 1 );
        }else {
            // Have gotten the permission
            startCaptureActivityForResult();
        }
    }

    private void startCaptureActivityForResult() {
        Intent intent=new Intent( this, CaptureActivity.class );
        Bundle bundle=new Bundle();
        bundle.putBoolean( CaptureActivity.KEY_NEED_BEEP, CaptureActivity.VALUE_BEEP );
        bundle.putBoolean( CaptureActivity.KEY_NEED_VIBRATION, CaptureActivity.VALUE_VIBRATION );
        bundle.putBoolean( CaptureActivity.KEY_NEED_EXPOSURE, CaptureActivity.VALUE_NO_EXPOSURE );
        bundle.putByte( CaptureActivity.KEY_FLASHLIGHT_MODE, CaptureActivity.VALUE_FLASHLIGHT_AUTO );
        bundle.putByte( CaptureActivity.KEY_ORIENTATION_MODE, CaptureActivity.VALUE_ORIENTATION_AUTO );
        bundle.putBoolean( CaptureActivity.KEY_SCAN_AREA_FULL_SCREEN, CaptureActivity.VALUE_SCAN_AREA_FULL_SCREEN );
        bundle.putBoolean( CaptureActivity.KEY_NEED_SCAN_HINT_TEXT, CaptureActivity.VALUE_SCAN_HINT_TEXT );
        intent.putExtra( CaptureActivity.EXTRA_SETTING_BUNDLE, bundle );
        startActivityForResult( intent, CaptureActivity.REQ_CODE );
    }

    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult( requestCode, permissions, grantResults );
        switch (requestCode) {
            case 1:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    startCaptureActivityForResult();
                } else {
                    Toast.makeText( simpleZxingActivity.this, "拒绝", Toast.LENGTH_LONG ).show();
                }
            default:
                break;
        }

    }

    // 扫码onActivityResult
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case CaptureActivity.REQ_CODE:
                mSealData = data.getStringExtra(CaptureActivity.EXTRA_SCAN_RESULT);
                switch (resultCode) {
                    case RESULT_OK:
                        AlertDialog.Builder builder = new AlertDialog.Builder(this);
                        builder.setCancelable( false );
                        builder.setTitle("提示")
                                .setMessage("铅封号：" + recode(mSealData))
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {

                                    }
                                })
                                .setNegativeButton( "取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {

                                    }
                                } )
                                .create().show();
                        break;
                    case RESULT_CANCELED:
                        if (data != null) {
                            // for some reason camera is not working correctly
                            Log.d(TAG,data.getStringExtra(CaptureActivity.EXTRA_SCAN_RESULT));
                        }
                        break;
                }
                break;
        }

    }

    //防止二维码扫描中文乱码
    private String recode(String str) {
        String formart = "";

        try {
            boolean ISO = Charset.forName("ISO-8859-1").newEncoder()
                    .canEncode(str);
            if (ISO) {
                formart = new String(str.getBytes("ISO-8859-1"), "GB2312");
            } else {
                formart = str;
            }
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return formart;
    }
}
